package com.px.task.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.px.common.annotation.Log;
import com.px.common.core.controller.BaseController;
import com.px.common.core.domain.AjaxResult;
import com.px.common.core.page.TableDataInfo;
import com.px.common.enums.BusinessType;
import com.px.common.utils.DateUtils;
import com.px.common.utils.poi.ExcelUtil;
import com.px.framework.aes.AesSecurityParameter;
import com.px.task.domain.TaskCalendar;
import com.px.task.enums.TaskCalendarType;
import com.px.task.service.ITaskCalendarService;
import com.px.task.vo.TaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 任务管理Controller
 * 
 * @author WRJ
 * @date 2024-08
 */
@Api("巡航任务管理")
@RestController
@RequestMapping("/task/calendar")
public class TaskCalendarController extends BaseController
{
    @Autowired
    private ITaskCalendarService taskCalendarService;

    /**
     * 查询任务管理列表
     */
    @ApiOperation("查询任务管理列表")
    @PreAuthorize("@ss.hasPermi('task:calendar:query')")
    @GetMapping("/list")
    @AesSecurityParameter(outEncode = true)
    public TableDataInfo list(TaskCalendar taskCalendar)
    {
        startPage();
        List<TaskCalendar> list = taskCalendarService.selectTaskCalendarList(taskCalendar);
        return getDataTable(list);
    }

    /**
     * 导出任务管理列表
     */
    @ApiOperation("导出任务管理列表")
    @PreAuthorize("@ss.hasPermi('task:calendar:query')")
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @AesSecurityParameter
    public void export(HttpServletResponse response, TaskCalendar taskCalendar)
    {
        List<TaskCalendar> list = taskCalendarService.selectTaskCalendarList(taskCalendar);
        ExcelUtil<TaskCalendar> util = new ExcelUtil<TaskCalendar>(TaskCalendar.class);
        util.exportExcel(response, list, "任务管理数据");
    }

    /**
     * 获取任务管理详细信息
     */
    @ApiOperation("获取任务管理详细信息")
    @PreAuthorize("@ss.hasPermi('task:calendar:query')")
    @GetMapping(value = "/{id}")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taskCalendarService.selectTaskCalendarById(id));
    }

    /**
     * 新增任务管理
     */
    @ApiOperation("新增任务管理")
    @PreAuthorize("@ss.hasPermi('task:calendar:edit')")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping
    @AesSecurityParameter(outEncode = true)
    public AjaxResult add(@RequestBody TaskCalendar taskCalendar)
    {
        if (null != taskCalendar.getTaskValidTime()) {
            taskCalendar.setTaskValidTime(DateUtil.beginOfDay(taskCalendar.getTaskValidTime()));
        }
        if (null != taskCalendar.getTaskInvalidTime()) {
            taskCalendar.setTaskInvalidTime(DateUtil.endOfDay(taskCalendar.getTaskInvalidTime()));
        }
        if (TaskCalendarType.NOW.getType() == taskCalendar.getTaskType()) {
            taskCalendar.setTaskEstimatedTime(new Date());
        }
        return toAjax(taskCalendarService.insertTaskCalendar(taskCalendar));
    }

    /**
     * 修改任务管理
     */
    @ApiOperation("修改任务管理")
    @PreAuthorize("@ss.hasPermi('task:calendar:edit')")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @AesSecurityParameter(outEncode = true)
    public AjaxResult edit(@RequestBody TaskCalendar taskCalendar)
    {
        if (null != taskCalendar.getTaskValidTime()) {
            taskCalendar.setTaskValidTime(DateUtil.beginOfDay(taskCalendar.getTaskValidTime()));
        }
        if (null != taskCalendar.getTaskInvalidTime()) {
            taskCalendar.setTaskInvalidTime(DateUtil.endOfDay(taskCalendar.getTaskInvalidTime()));
        }
        if (TaskCalendarType.NOW.getType() == taskCalendar.getTaskType()) {
            taskCalendar.setTaskEstimatedTime(new Date());
        }
        return toAjax(taskCalendarService.updateTaskCalendar(taskCalendar));
    }

    /**
     * 删除任务管理
     */
    @ApiOperation("删除任务管理")
    @PreAuthorize("@ss.hasPermi('task:calendar:remove')")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(taskCalendarService.deleteTaskCalendarByIds(ids));
    }

    /**
     * 获取指定月份所有任务列表
     */
    @ApiOperation("获取指定月份所有任务列表")
    @PreAuthorize("@ss.hasPermi('task:calendar:query')")
    @GetMapping(value = "monthTask")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult monthTaskList(@ApiParam(name = "查询月份,格式yyyy-MM",value = "month",required = true) @RequestParam("month") String month)
    {
        Date date;
        try {
            date = DateUtils.parseDate(month, "yyyy-MM");
        } catch (ParseException e) {
            return AjaxResult.error("查询参数格式不是yyyy-MM");
        }
        List<TaskVo> tasks = taskCalendarService.getMonthTaskByMonthAndType(date);

        List<JSONObject> result = taskCalendarService.processResultFormat(tasks);
        return AjaxResult.success(result);
    }

}
